package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.PermissionGroupMapper;
import com.itheima.pojo.PePermissionGroup;
import com.itheima.service.PermissionGroupService;
import com.itheima.util.PageResult;
import com.itheima.util.PesGroup;
import com.itheima.util.PesGroup2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PermissionGroupServiceImpl implements PermissionGroupService {
    @Autowired
    PermissionGroupMapper permissionGroupMapper;
    @Override
    public PageResult findPage(Integer page, Integer pagesize, String keyword) {
        LambdaQueryWrapper<PePermissionGroup> wrapper = new LambdaQueryWrapper<>();
        if(keyword!=null)
        wrapper.like(PePermissionGroup::getName,keyword);
        wrapper.orderByDesc(PePermissionGroup::getCreateTime);
        IPage<PePermissionGroup> p=new Page<>(page,pagesize);
        IPage<PePermissionGroup> pg= permissionGroupMapper.selectPage(p, wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setList(pg.getRecords());
        pageResult.setCounts(Integer.parseInt(pg.getTotal()+""));
        pageResult.setPage(page+"");
        pageResult.setPages(Integer.parseInt(pg.getPages()+""));
        pageResult.setPagesize(pagesize+"");
        return pageResult;
    }

    @Override
    public void del(Integer id) {
        permissionGroupMapper.delPG(id);
        permissionGroupMapper.deleteById(id);
    }

    @Override
    public void add(PesGroup pesGroup) {
        PePermissionGroup pePermissionGroup = new PePermissionGroup();
        pePermissionGroup.setCreateTime(new Date());
        pePermissionGroup.setUpdateTime(new Date());
        pePermissionGroup.setName(pesGroup.getTitle());
        permissionGroupMapper.insert(pePermissionGroup);
        for (Integer pid : pesGroup.getPermissions()) {
            permissionGroupMapper.addPG(pid,pePermissionGroup.getId());
        }
    }

    @Override
    public List sim(String keyword) {
        LambdaQueryWrapper<PePermissionGroup> wrapper = new LambdaQueryWrapper<>();
        if(keyword!=null)
            wrapper.like(PePermissionGroup::getName,keyword);
        List<PePermissionGroup> pePermissionGroups = permissionGroupMapper.selectList(wrapper);
        return pePermissionGroups;
    }

    @Override
    public void updateById(Integer id, PesGroup pesGroup) {
        permissionGroupMapper.delPG(id);
        PePermissionGroup pePermissionGroup = permissionGroupMapper.selectById(id);
        pePermissionGroup.setName(pesGroup.getTitle());
        pePermissionGroup.setUpdateTime(new Date());
        permissionGroupMapper.updateById(pePermissionGroup);
        Integer[] permissions = pesGroup.getPermissions();
        for (Integer permission : permissions) {
            permissionGroupMapper.addPG(permission,pePermissionGroup.getId());
        }
    }

    @Override
    public PePermissionGroup detail(Integer id) {
        PePermissionGroup pePermissionGroup = permissionGroupMapper.selectById(id);
        Integer[] p=permissionGroupMapper.selPG(id);
        pePermissionGroup.setPermissions(p);
        return pePermissionGroup;
    }
}
